package cn.onesdream.app.api.controller;

import cn.onesdream.app.api.service.ChatService;
import cn.onesdream.app.api.service.UserService;
import cn.onesdream.app.common.util.ResponseResult;
import cn.onesdream.app.dao.entity.Chat;
import cn.onesdream.app.dao.entity.User;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = "聊天相关接口")
@RequestMapping("/api/app/chat")
@Validated
public class ChatController {
    @Autowired
    private ChatService chatService;

    @Autowired
    private UserService userService;

    @ApiOperation("获取历史消息")
    @GetMapping("/listHistoryMsg")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fromUserId", value = "来源用户id"),
            @ApiImplicitParam(name = "beginDate", value = "开始日期，yyyy-MM-dd,可不传"),
            @ApiImplicitParam(name = "endDate", value = "结束日期，yyyy-MM-dd,可不传"),
            @ApiImplicitParam(name = "current", value = "当前页码"),
            @ApiImplicitParam(name = "size", value = "页码容量")
    })
    public ResponseResult listHistoryMsg(Integer fromUserId, String beginDate, String endDate, Integer size, Integer current){
        String myAccount = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = userService.getUserByAccount(myAccount);
        Page<Chat> historyMsg = chatService.listHistoryMsg(user.getId(), fromUserId, beginDate, endDate, size, current);
        for (Chat msg : historyMsg.getRecords()){
            chatService.readMsg(msg.getId());
        }
        return new ResponseResult(historyMsg);
    }

    @ApiOperation("获取未读消息")
    @GetMapping("/listUnreadMsg")
    public ResponseResult listUnreadMsg(Integer current, Integer size){
        String myAccount = SecurityContextHolder.getContext().getAuthentication().getName();
        User toUser = userService.getUserByAccount(myAccount);
        Page<Chat> unreadMsg = chatService.listUnreadMsg(toUser.getId(), current, size);
        //返回前端自定义chatBox所需的数据
        List<Chat> list = unreadMsg.getRecords();
        List<HashMap<String, Object>> listChat = new ArrayList<>();
        for (Chat chat : list){
            HashMap<String, Object> chatRecord = new HashMap<>();
            Integer id = chat.getId();
            Integer fromUserId = chat.getFromUserId();
            User fromUser = userService.getUserById(fromUserId);
            chatRecord.put("headImgUrl", fromUser.getHeadImgUrl());
            chatRecord.put("nickname", fromUser.getNickname());
            chatRecord.put("createTime", chat.getCreateTime());
            chatRecord.put("content", chat.getContent());
            chatRecord.put("fromUserId", chat.getFromUserId());
            chatService.readMsg(id);
            listChat.add(chatRecord);
        }
        return new ResponseResult(listChat);
    }

    @ApiOperation("将信息全部标记为已读")
    @PutMapping("/isReadUserMsg")
    @ApiImplicitParam(name = "fromUserId", value = "对方用户id")
    public ResponseResult isReadUserMsg(Integer fromUserId){
        chatService.isReadUserMsg(fromUserId);
        return new ResponseResult();
    }
}
